package cn.marinda.totipotentwar;

import cn.marinda.totipotentwar.command.Commands;
import cn.marinda.totipotentwar.global.*;
import cn.marinda.totipotentwar.listener.InventoryListener;
import cn.marinda.totipotentwar.listener.PlayerListener;
import cn.marinda.totipotentwar.manager.GameDataManager;
import cn.marinda.totipotentwar.runnable.DelayMoveRunnable;
import cn.marinda.totipotentwar.runnable.GameRunnable;
import cn.marinda.totipotentwar.utils.TimeUtil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.CommandMap;
import org.bukkit.command.PluginCommand;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.SimplePluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public final class TotipotentWar extends JavaPlugin {
    public static ConfigData configData;
    public static RankData rankData;
    /**
     * 插件
     */
    public static TotipotentWar plugin;
    public static ScoreData scoreData;
    public static ScoreBoardData scoreBoardData;
    public static ShopData shopData;
    public static WeaponsData weaponsData;
    public static BukkitTask bukkitTask;
    public static BukkitTask delayTask;
    public static GameDataManager gameDataManager;
    @Override
    public void onEnable() {
        // Plugin startup logic
        registerCommands();
        plugin = this;
        defaultConfigSource();
        configData = new ConfigData(this);
        gameDataManager = GameDataManager.instance;
        rankData = new RankData();
        weaponsData = new WeaponsData();
        scoreData = new ScoreData();
        scoreBoardData = new ScoreBoardData();
        shopData = new ShopData();
        //注册控制器
        bukkitTask = new GameRunnable(configData).runTaskTimerAsynchronously(this,0,20);
        delayTask =  new DelayMoveRunnable().runTaskTimerAsynchronously(TotipotentWar.plugin,0,20);
        Bukkit.getPluginManager().registerEvents(new InventoryListener(),this);
        Bukkit.getPluginManager().registerEvents(new PlayerListener(),this);
        Bukkit.getConsoleSender().sendMessage("§a[TotipotenWar]插件已加载完毕");;
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
        Bukkit.getConsoleSender().sendMessage("§a[TotipotenWar]插件卸载完毕");
    }

    /**
     * 注册命令
     */
    private void registerCommands(){
        try{
            //自定义注册第一步
            Constructor<PluginCommand> constructor =  PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
            //通过暴力破解无视修饰符
            constructor.setAccessible(true);
            PluginCommand pluginCommand = constructor.newInstance("twar",this);
            List<String> alignsList = new ArrayList<>();

            alignsList.add("twar");
            pluginCommand.setDescription("一个小游戏插件。");
            pluginCommand.setAliases(alignsList);
            pluginCommand.setDescription("plugin description");
            pluginCommand.setExecutor(new Commands());
            Field commandMap = SimplePluginManager.class.getDeclaredField("commandMap");
            commandMap.setAccessible(true);

            CommandMap commandMap1 = (CommandMap) commandMap.get(this.getServer().getPluginManager());
            commandMap1.register("twar",pluginCommand);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 默认的配置源
     */
    private void defaultConfigSource(){
        saveDefaultConfig();
        saveResource("score.yml",false);
        saveResource("weapons.yml",false);
        saveResource("rank.yml",false);
        saveResource("scoreboard.yml",false);
        saveResource("shop.yml",false);
    }
}
